package com.centit.suixuan.puremvc.view
{
	import com.centit.suixuan.puremvc.model.User;
	import com.centit.suixuan.puremvc.model.UserProxy;
	
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	
	import org.osmf.containers.MediaContainer;
	import org.osmf.layout.LayoutMetadata;
	import org.osmf.layout.LayoutRendererBase;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import spark.components.Button;
	
	public class LoginMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "LoginMediator";
		public static const LOGIN_SUBMIT:String = "LoginSubmit";
		public static const LOGIN_SUCCESSFUL:String = "LoginSuccessful";
		public static const LOGIN_ERROR:String = "LoginError";
		
		
		public function LoginMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
			(viewComponent.btnSubmit as Button).addEventListener(MouseEvent.CLICK,onSubmit);
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var loginPanel : LoginPureMVC = notification.getBody() as LoginPureMVC;
			switch(notification.getName()){
				case LOGIN_SUBMIT :    new UserProxy(UserProxy.NAME, loginPanel).login(new User(loginPanel.txtUserName.text, loginPanel.txtPassword.text)); break;
				case LOGIN_SUCCESSFUL : Alert.show("登陆成功");break;
				case LOGIN_ERROR : loginPanel.lblTitle.text = "用户名或密码错误";break;
				default : break;
			}
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				LOGIN_SUBMIT,
				LOGIN_ERROR,
				LOGIN_SUCCESSFUL
			];
		}
		
		
		
		
		
		private function onSubmit (e : MouseEvent): void{
			this.sendNotification(LOGIN_SUBMIT, viewComponent);
		}
	}
}